<?php
// +----------------------------------------------------------------------
// | OneThink [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.onethink.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: 麦当苗儿 <zuojiazi@vip.qq.com> <http://www.zjzit.cn>
// +----------------------------------------------------------------------

namespace Admin\Controller;
/**
 * 后台首页控制器
 * @author 麦当苗儿 <zuojiazi@vip.qq.com>
 */
class YwxtController extends AdminController {
    public function __construct (){
        parent::__construct();
        $ywlb   = D('Ywlb');
        //获取权限
        $quanxian = $ywlb->quanxian();
        $this->assign('quanxian',$quanxian);
        //查询业务状态列表
        $state  = $ywlb->yw();
        $this->assign('state',$state);

        //查询渠道
        $qudao  = $ywlb->qd();
        $this->assign('qudao',$qudao);

        //查询类别
        $liebei = $ywlb->lb();
        $this->assign('liebei',$liebei);

		//支付方式
        $zffs  = $ywlb->zffs();
        $this->assign('zffs',$zffs);
		
		//支付款项
        $zfkx  = $ywlb->zfkx();
        $this->assign('zfkx',$zfkx);
		
		//顾客标签
	    $gkbq = $ywlb->gkbq();
        $this->assign('gkbq',$gkbq);
		
        //客服
        $kefu = kefu();
        $this->assign('kefu',$kefu);


        //线下
        $xianxia = xianxia();
        $this->assign('xianxia',$xianxia);

        //线上
        $xianshang = xianshang();
        $this->assign('xianshang',$xianshang);
		
		//挖潜部门
        $wqbm = M('waqian')->where(['pid'=>0,'status'=>1])->select();
        $this->assign('wqbm',$wqbm);
		
		//挖潜人员
        $wqry = M('waqian')->where(['pid'=>['neq',0],'status'=>1])->select();
        $this->assign('wqry',$wqry);
		
	

    }

    /**
     * 业务系统
     */
    public function index($yytime_start = '',$yytime_end = '',$yytime = '',$jdtime = '',$kf = '' ,$xsjd = '',$xxjd = '',$ywstate = '',$qdstase = '',$cztime = '',$money = '',$lb = ''){
        $this->meta_title = '业务系统';
        $title       =   I('title');
		$jrzjtx      =    I('jrzjtx');
		$jdlstx      =    I('jdlstx');
		$map=array();
		if(!empty($jrzjtx)){
			$maps = $this->jrzjtx();
			$map  = $maps['map'];
			
			session('map',$map);
		}
		if(!empty($jdlstx)){
			$maps = $this->jdlstx();
			$map  = $maps['map'];
			
			session('map',$map);
		}
        //查询数据接收包
		if ($_POST['Reserve']['id']) {
            $map['id'] = array('eq',$_POST['Reserve']['id']);
        }
        if ($_POST['Reserve']['ddbh']) {
            $map['ddbh'] = array('eq',$_POST['Reserve']['ddbh']);
        }
        if ($_POST['Reserve']['name']) {
            $map['name'] = array('LIKE','%'.$_POST['Reserve']['name'].'%');
        }
		if ($_POST['Reserve']['qq']) {
            //$map['qq'] = array('eq',$_POST['Reserve']['qq']);
			$map['qq'] = array('LIKE','%'.$_POST['Reserve']['qq'].'%');
        }
        if ($_POST['Reserve']['phone']) {
            //$map['phone'] = array('eq',$_POST['Reserve']['phone']);
			$map['phone'] = array('LIKE','%'.$_POST['Reserve']['phone'].'%');
        }
		if ($_POST['Reserve']['waqianren']) {
			$map['waqianren'] = array('LIKE','%'.$_POST['Reserve']['waqianren'].'%');
        }
        if ($_POST['Reserve']['jdtime_start'] && $_POST['Reserve']['jdtime_end']) {
            $map['jdtime'] = array('between',array(strtotime($_POST['Reserve']['jdtime_start']),strtotime($_POST['Reserve']['jdtime_end'])));
        }
        if ($_POST['Reserve']['yytime_start'] && $_POST['Reserve']['yytime_end']) {
            $map['yytime'] = array('between',array(strtotime($_POST['Reserve']['yytime_start'].'00:00:00'),strtotime($_POST['Reserve']['yytime_end'].'23:59:59')));
        }
		if ($_POST['Reserve']['jindtime_start'] && $_POST['Reserve']['jindtime_end']) {
            $map['jindtime'] = array('between',array(strtotime($_POST['Reserve']['jindtime_start']),strtotime($_POST['Reserve']['jindtime_end'].'23:59:59')));
        }
		if ($_POST['Reserve']['wddtime_start'] && $_POST['Reserve']['wddtime_end']) {
            $map['wddtime'] = array('between',array(strtotime($_POST['Reserve']['wddtime_start'].'00:00:00'),strtotime($_POST['Reserve']['wddtime_end'].'23:59:59')));
        }
		if ($_POST['Reserve']['lstime_start'] && $_POST['Reserve']['lstime_end']) {
            $map['lstime'] = array('between',array(strtotime($_POST['Reserve']['lstime_start'].'00:00:00'),strtotime($_POST['Reserve']['lstime_end'].'23:59:59')));
        }
   
        if ($_POST['Reserve']['ddtime_start'] && $_POST['Reserve']['ddtime_end']) {
            $map['ddtime'] = array('between',array(strtotime($_POST['Reserve']['ddtime_start'].'00:00:00'),strtotime($_POST['Reserve']['ddtime_end'].'23:59:59')));
        }
		
		if ($_POST['Reserve']['xxfwtime_start'] && $_POST['Reserve']['xxfwtime_end']) {
            $map['xxfwtime'] = array('between',array(strtotime($_POST['Reserve']['xxfwtime_start']),strtotime($_POST['Reserve']['xxfwtime_end'])));
        }
        if ($_POST['Reserve']['kf']) {
            $map['kf'] = array('eq',$_POST['Reserve']['kf']);
        }
        if ($_POST['Reserve']['xsjd']) {
            $map['xsjd'] = array('eq',$_POST['Reserve']['xsjd']);
        }
        if ($_POST['Reserve']['xxjd']) {
            $map['xxjd'] = array('eq',$_POST['Reserve']['xxjd']);
        }
        if ($_POST['Reserve']['qd']) {
            $map['qd'] = array('eq',$_POST['Reserve']['qd']);
        }
        if ($_POST['Reserve']['ywstate']) {
            $map['ywstate'] = array('eq',$_POST['Reserve']['ywstate']);
        }
        if ($_POST['Reserve']['lb']) {
            $map['lb'] = array('eq',$_POST['Reserve']['lb']);
        }
		if ($_POST['Reserve']['gkbq']) {
            $map['gkbq'] = array('eq',$_POST['Reserve']['gkbq']);
        }
		if ($_POST['Reserve']['wqbm']) {
            $map['wqbm'] = array('eq',$_POST['Reserve']['wqbm']);
        }
		if ($_POST['Reserve']['wqry']) {
            $map['wqry'] = array('eq',$_POST['Reserve']['wqry']);
        }
		if ($_POST['Reserve']['akjj']) {
            $map['akjj'] = array('eq',$_POST['Reserve']['akjj']);
        }
		if ($_POST['Reserve']['cjjj']) {
            $map['cjjj'] = array('eq',$_POST['Reserve']['cjjj']);
        }
		
		
        //判断正序、倒序查询
        if ($yytime){
            $paixu['id'] = 'yytime';
            $paixu['state'] =  $yytime;
        }
        if ($jdtime){
            $paixu['id'] = 'jdtime';
            $paixu['state'] =  $jdtime;
        }
        if ($kf){
            $paixu['id'] = 'kf';
            $paixu['state'] =  $kf;
        }
        if ($xsjd){
            $paixu['id'] = 'xsjd';
            $paixu['state'] =  $xsjd;
        }
        if ($xxjd){
            $paixu['id'] = 'xxjd';
            $paixu['state'] =  $xxjd;
        }
        if ($cz){
            $paixu['id'] = 'cz';
            $paixu['state'] =  $cz;
        }
        if ($ywstate){
            $paixu['id'] = 'ywstate';
            $paixu['state'] =  $ywstate;
        }
        if ($qdstase){
            $paixu['id'] = 'qd';
            $paixu['state'] =  $qdstase;
        }
        if ($lb){
            $paixu['id'] = 'lb';
            $paixu['state'] =  $lb;
        }
        if ($money){
            $paixu['id'] = 'money';
            $paixu['state'] =  $money;
        }
		//接收所有表单数据
		$post_Reserve  =I('post.Reserve');

		//判断是否提交数据，如果提交数据则存入session ，没有数据判断是否进入分页，不是分页则清空session
		if(IS_POST){
			session('map',$map);
			session('Reserve',$post_Reserve);
		}elseif($yytime_start && $yytime_end){
            $map['yytime'] = array('between',array($yytime_start,$yytime_end));
			session('map',$map);
		}elseif(!I('p')){
			session('map',null);
			session('Reserve',null);
		}
		 $values = session('map');
        //排序
		
        if($paixu['state'] == asc){
            $paixuid = $paixu['id'];
            $data_yw=$this->lists('Yewu',$values,"$paixuid asc");
        }else if($paixu['state'] == desc){
            $paixuid = $paixu['id'];
            $data_yw=$this->lists('Yewu',$values,"$paixuid desc");
        }else if(!empty($values['yytime'])){
			$data_yw=$this->lists('Yewu',$values,"yytime asc");
		}else{
            $data_yw=$this->lists('Yewu',$values,"id desc");
        }
        $ywstate = D('Ywlb')->where(array('status'=>array('eq',1)))->field('uid,name')->select();
        $kf = D('Member')->field('uid,nickname')->select();
		$wqbm = D('Waqian')->where(['status'=>1])->field('id,title')->select();
        //重组数据获取客服、接待、状态名称
        foreach ($data_yw as $key => $value) {
			$value['ywzt']    =  $value['ywstate'];
            foreach ($ywstate as $key => $v) {
                if ($value['ywstate'] == $v['uid']) {
                   $value['ywstate'] = $v['name'];
                }
                if ($value['qd'] == $v['uid']) {
                   $value['qd'] = $v['name'];
                }
                if ($value['lb'] == $v['uid']) {
                   $value['lb'] = $v['name'];
                }
				if ($value['gkbq'] == $v['uid']) {
                   $value['gkbq'] = $v['name'];
                }
            }
            foreach ($kf as $key => $s) {
                if ($value['kf'] == $s['uid']) {
                   $value['kf'] = $s['nickname'];
                }
                if ($value['xsjd'] == $s['uid']) {
                   $value['xsjd'] = $s['nickname'];
                }
                if ($value['xxjd'] == $s['uid']) {
                   $value['xxjd'] = $s['nickname'];
                }
            }
			foreach ($wqbm as $key => $wq) {
                if ($value['wqbm'] == $wq['id']) {
                   $value['wqbm'] = $wq['title'];
                }
				if ($value['wqry'] == $wq['id']) {
                   $value['wqry'] = $wq['title'];
				}   
            }
			
            $data[] = $value;
        }
        int_to_string($data);
        $this->assign('_list',$data);
		//统计套系金额和实收金额。
        foreach ($data as $k=>$v){
			$zhongji['money'] += $v['money'];
			$zhongji['setmoney'] += $v['setmoney'];
		}
		$this->assign('zhongji',$zhongji);
		
        //业务查询记录参数。
        //$post_data  = I('post.Reserve');
		$post_data  = session('Reserve');
        $this->assign('post_data',$post_data);

        //时间生成
        $jrdate = date("Y-m-d", time());
        $jryydate['start'] = strtotime($jrdate."0:0:0");
        $jryydate['end'] = strtotime($jrdate."23:59:59");
        $this->assign('jryydate',$jryydate); 
        $this->display();
    }
    /**
     * 添加
     * @author HuangLuSen <274799010@qq.com>
     */
    public function add(){
        $quanxian = D('Ywlb')->quanxian();
        if(IS_POST){
            $yewu = M('Yewu');
            $data = $yewu->create();
            $data['ddbh']   = order_id();
            $data['yytime'] = strtotime($data['yytime']);
			$data['pdtimes'] = strtotime($data['pdtimes']);
			$data['pdtimee'] = strtotime($data['pdtimee']);
			$data['xxfwtime'] = strtotime($data['xxfwtime']);
            $data['cztime'] = time();
            $data['jdtime'] = time();
			//判断成交状态，添加未到店时间	
			if($data['ywstate'] == 64){
				$data['wddtime'] = time(); 
			}
			//判断成交状态，添加成交时间	
			if($data['ywstate'] == 67 || $data['ywstate'] == 115 || $data['ywstate'] == 151){
				$data['ddtime'] = time(); 
			}
			//判断流失状态，添加流失时间
			if($data['ywstate'] == 65 || $data['ywstate'] == 149){
				$data['lstime'] = time(); 
			}
			//判断死单状态，添加死单时间
			if($data['ywstate'] == 153){
				$data['sdtime'] = time(); 
			}
			//判断进店状态，添加进店时间
			if($data['ywstate'] == 115 || $data['ywstate'] == 148 || $data['ywstate'] == 149){
				$data['jindtime'] = time(); 
			}
            $fenpeiweek = date('w',$data['yytime']);
            $fenpeihour = date('H',$data['yytime']);
            $fenpeidate = date('Y-m-d',$data['yytime']);
            if($data['yytime']){
                $fenpeiyytime = true;
            }
            $paidan = get_paidan($fenpeiweek,$fenpeihour,$fenpeidate,$fenpeiyytime);
            if($paidan){
				if(!empty($data['phone'])){
					$maps['phone'] = array('eq',$data['phone']);
					$phones = $yewu->where($maps)->count();
				}
				if(empty($phones)){
					  $res = $yewu->add($data);  
					if($res){
						if($data['ywstate'] == 33){//判断业务系统是否等于63（确认意向）
								$wxbb   = new WxbbController;//实例化微信分配接口
								$wxbb->fenpei($res);  //传送业务ID、发送者级别。
							}
					   //记录用户行为
					   action_log('order_add', 'order', $res , UID ,$data['ywstate']);	
					   $this->success('添加成功！',U('Ywxt/index'));         
					}else{
						$this->error('添加失败！');
					}					  
				}else{
					$this->error('电话号码重复！');
				}
                 
      
            }else{
                $this->error('您排预约时间已满,请换其他时间段!');
            }
        
        }
        //显示权限，负责网页每个用户显示内容的锁定
		
		if($quanxian === 4 || $quanxian === 5 || $quanxian === 8){
            $xsqx = 'disabled="false"';
            $this->assign('kfqx', $kfqx);
        } 		
        $this->meta_title = '新建预约单';
        $this->display();
   
    }

    /**
     * 编辑文章
	 * $state 查询本单的状态。
     */
    public function edit($id = 0){
        $Works    = D('Yewu');
        $quanxian = D('Ywlb')->quanxian();
        $state    = $Works->field('zfkx,yytime,ywstate')->find($id);
		
       // $arr_a    = array('33','63','64','65','66','148','149','151','152','153');
       //成交状态改成支付款项
        //$arr_a    = array('131','132','0');
     
        if($quanxian === 5 || $quanxian === 8 || $quanxian === 4){
          //  if(!in_array($state['zfkx'], $arr_a)){
          //          $this->error('顾客已成交您没有权限修改订单！'); 
          //  }
			if($state['zfkx'] == 130){
                    $this->error('顾客已成交您没有权限修改订单！'); 
            }
        }
		$arr_b   = array('64','10000');//判断未到店
		$arr_c   = array('65','149');//判断流失数组
		$arr_d   = array('152','153');//判断死单数组
		$arr_e   = array('115','67','151');//判断成交数组
		$arr_f   = array('115','148','149','153');//判断进店数组
        /*权限判断
        * 线上管理权限ADMIN、老板、客服、线上门市、线上经理
        * 3、门店经理  4、获取客服 5、线下门市 6、线上经理 7、老板 8、线上门市
        */
        if($quanxian === 5 ){
            $xsqx = 'disabled="false"';
            $this->assign('xsqx', $xsqx);
        } 
        if($quanxian === 3 || $quanxian === 5 || $quanxian === 4 || $quanxian === 8){    
            $xsjl = 'disabled="false"';
            $this->assign('xsjl', $xsjl);
        }
        if($quanxian === 3 || $quanxian === 5 || $quanxian === 8){
            $huoqu = 'disabled="false"';
            $this->assign('huoqu',$huoqu);
        }   


        if(IS_POST){
            $data = $Works->create();
            $p    = I('p');
		
            if($data){
                $data['cztime'] = time();
                $data['yytime'] = strtotime($data['yytime']);
				$data['xxfwtime'] = strtotime($data['xxfwtime']);
				$data['pdtimes'] = strtotime($data['pdtimes']);//排单开始时间
				$data['pdtimee'] = strtotime($data['pdtimee']);//排单结束时间
				
				if(in_array($state['ywstate'],$arr_b,true)){
					
				}else{
					//判断成交状态，添加未到店时间	
					if($data['ywstate'] == 64){
						$data['wddtime'] = time(); 
					}
				}
				if(in_array($state['ywstate'],$arr_e,true)){
					
				}else{				
					//判断成交状态，添加成交时间	
					if($data['ywstate'] == 67 || $data['ywstate'] == 115 || $data['ywstate'] == 151){
						$data['ddtime'] = time(); 
					}
				}
				if(in_array($state['ywstate'],$arr_c,true)){
					
				}else{		
					//判断流失状态，添加流失时间
					if($data['ywstate'] == 65 || $data['ywstate'] == 149){
						$data['lstime'] = time(); 
					}
				}
				if(in_array($state['ywstate'],$arr_d,true)){
					
				}else{	
					//判断死单状态，添加死单时间
					if($data['ywstate'] == 152 || $data['ywstate'] == 153){
						$data['sdtime'] = time(); 
					}
				}
				if(in_array($state['ywstate'],$arr_f,true)){
					
				}else{	
					//判断进店状态，添加进店时间
					if($data['ywstate'] == 115 || $data['ywstate'] == 148 || $data['ywstate'] == 149 || $data['ywstate'] == 153){
						$data['jindtime'] = time(); 
					}
				}	
                //判断预约时间是否修改，如果修改判断排预约时间
                if($state['yytime'] == $data['yytime']){
                    $paidan = true;

                }else{
                    //排预约时间
                    $fenpeiweek = date('w',$data['yytime']);
                    $fenpeihour = date('H',$data['yytime']);
                    $fenpeidate = date('Y-m-d',$data['yytime']);
                    if($data['yytime']){
                        $fenpeiyytime = true;
                    }
                    
                    $paidan = get_paidan($fenpeiweek,$fenpeihour,$fenpeidate,$fenpeiyytime);
                }
                
                 
                if($paidan){
               
						$res =  $Works->save($data);

                    if($res){
						if(in_array($state['ywstate'],$arr_e)){
							
						}else{
							if($data['ywstate'] == 67 || $data['ywstate'] == 115 || $data['ywstate'] == 151){
                            //判断业务系统是否等于67/115（成交）
								$wxbb   = new WxbbController;//实例化微信分配接口
								$wxbb->cjtx($data['id']);  //传送业务ID、发送者级别。
							}
						}
						if(in_array($state['ywstate'],$arr_c)){
							
						}else{
							if($data['ywstate'] == 65 || $data['ywstate'] == 149){
								//判断业务系统是否等于65/149（流失）
									$wxbb   = new WxbbController;//实例化微信分配接口
									$wxbb->lstx($data['id']);  //传送业务ID、发送者级别。
							}
						}
						if(in_array($state['ywstate'],$arr_d)){
							
						}else{
							if($data['ywstate'] == 152 || $data['ywstate'] == 153){
								//判断业务系统是否等于152/153（流失）
									$wxbb   = new WxbbController;//实例化微信分配接口
									$wxbb->sdtx($data['id']);  //传送业务ID、发送者级别。
							}	
						}	
                        //记录用户行为
                        action_log('order_up', 'order', $data['id'] , UID ,$data['ywstate']);

                        if($p){
                            $this->success('更新成功', U('index',array('p'=>$p)));
                        }else{
                            $this->success('更新成功', U('index',array('p'=>1)));
                        }
                        
                    }else{
                        $this->error('更新失败');
                    } 
                }else{
                    $this->error('您排预约时间已满,请换其他时间段!');
                }
                
            } else {
                $this->error($Works->getError());
            }
        } else {
            $data = $Works->field(true)->find($id);
            $this->assign('data', $data);
            $this->meta_title = '订单编辑';
            $this->display('add');
        }
    }
    /**
     * 删除文章
     * @author HuangLuSen <274799010@qq.com>
     */
    public function del(){
        //$id = array_unique((array)I('id',0));
        $id = I('id');
		$p  = I('p');
        if ( empty($id) ) {
            $this->error('请选择要操作的数据!');
        }
        $map = array('id' => array('eq', $id) );
        $data = M('Yewu')->where($map)->find();
        M('YewuCopy')->add($data); 
        
        if(M('Yewu')->where($map)->delete()){
            //记录行为
            action_log('deldate_yewu', 'Yewu', $id, UID);
			if($p){
				$this->success('删除成功!',U('index',array('p'=>$p)));
			}else{
				$this->success('删除成功!',U('index',array('p'=>1)));
			}
        } else {
            $this->error('删除失败！');
        }
    }
    /**
     * 未到店
     */
    public function wdd($id = ''){
        $ywstate  =  M('Yewu')->field('ywstate')->find($id);
        $yw       =  $ywstate['ywstate'];
        $quanxian =  D('Ywlb')->quanxian();
        if($quanxian === 5 || $quanxian === 8){
            if($yw == 67 || $yw == 115){
                    $this->error('顾客已成交您没有权限修改订单！'); 
            }
        } 
        if ( empty($id) ) {
            $this->error('请选择要操作的数据!');
        }
        $map = array('id' => array('eq', $id) );
        $data['ywstate'] = '64'; 
        $data['cztime']  = time();
        $data['wddtime'] = time();
        if(M('Yewu')->where($map)->save($data)){
			//记录用户行为
            action_log('order_wdd', 'order', $id , UID);
            $this->success('修改状态成功!');
        } else {
            $this->error('修改状态失败!');
        }
    }
    /**
     * 流失
     * @author HuangLuSen <274799010@qq.com>
     */
    public function ls($id = ''){
		if (empty($id)){
			$this->error('请选择要操作的数据!');
		}
        $ywstate   =  D('Yewu')->field('ywstate')->find($id);
        $yw        =  $ywstate['ywstate']; 
		$quanxian =  D('Ywlb')->quanxian();
		if($quanxian === 5 || $quanxian === 8){
            if($yw == 67 || $yw == 115){
                    $this->error('顾客已成交您没有权限修改订单！'); 
            }
        } 
        $map = array('id' => array('eq', $id) );
        $data['ywstate'] = '65'; 
        $data['cztime']  = time();
        $data['lstime']  = time();
        if(M('Yewu')->where($map)->save($data)){
            //记录用户行为
            action_log('order_ls', 'order', $id , UID);
            $this->success('修改状态成功!');
        } else {
            $this->error('修改状态失败！');
        }
        
    }
    /**
     * 成交
     */
        public function cj(){  
        $rid = I('id');
        $data['ywstate'] = I('ywstate'); 
        $data['money'] 	 = I('money'); 
		$data['zffs'] 	 = I('zffs'); 
		$data['zfkx'] 	 = I('zfkx'); 
		$data['pdtimes']  = strtotime(I('pdtimes'));
		$data['pdtimee']  = strtotime(I('pdtimee')); 		
        $data['cztime']  = time();
        $data['ddtime']  = time();
		//判断是否线下成交，添加进店时间。
		if($data['ywstate'] == 115){
			$data['jindtime']  = time();
		}
        if(M('Yewu')->where(array('id'=>$rid))->save($data)){
			//记录用户行为
            action_log('order_cj', 'order', $rid, UID);
			if($data['ywstate'] == 67 || $data['ywstate'] == 115){//判断业务系统是否等于67/115（成交）
                        $wxbb   = new WxbbController;//实例化微信分配接口
                        $wxbb->cjtx($rid);  //传送业务ID、发送者级别。
            }
			$this->success('订单修改成功~~~');
            //$this->ajaxReturn('修改状态成功',U('index'));
        } else {
           // $this->error('修改状态失败！');
            $this->ajaxReturn('修改状态失败！');
        }
    }
     /**
     * 分配
     */
    public function fp($id = 0){
        $Works    =  D('Yewu');
        $ywstate  =  $Works->find($id);
        $yw       =  $ywstate['ywstate']; 
        $quanxian =  D('Ywlb')->quanxian();
        if($quanxian === 3 || $quanxian === 6){
            if($yw == 67 || $yw == 115){
                    $this->error('顾客已成交您没有权限修改订单！'); 
            }
        }     

        if(IS_POST){
            $data = $Works->create();
            $data['cztime']=time();
            $data['id'] = I('id');
            if($Works->where(array('id'=>array('eq',$data['id'])))->save($data)!== false){
                session('ADMIN_MENU_LIST',null);
                //微信通知接待门市
                // $wxbb   = new WxbbController; //实例化微信类别
                // if($data['ywstate'] == 33){//判断业务系统是否等于33（待确定）
                //     if($quanxian === 6){  //判断线上经理
                //         $wxbb->xsfendan($data['id']);  //发送微信提醒到线上门市
                //     }
                // }
                // if($data['ywstate'] == 63){//判断业务系统是否等于63（确认意向）
                //     if($quanxian === 3){  //判断下线经理
                //         $wxbb->xxfendan($data['id']); 
                //     }else{
                //         $wxbb->fenpei($data['id'],3);    
                //     }
                // }
				//记录用户行为
                action_log('order_fp', 'order', $data['id'], UID );
                $this->success('更新成功', U('index'));
            } else {
                $this->error('更新失败');
            }
              
        }else{
            $data = $Works->field(true)->find($id);
            $this->assign('data', $data);
        }
        $this->meta_title = '订单编辑';
        $this->display('fp');
    }
    /**
     * 查看
     * @author HuangLuSen <274799010@qq.com>
     */
    public function cat($id = 0){

        $Works  = M('Yewu');
        $data   = $Works->field(true)->find($id);
		$this->assign('data', $data);
		//日志查询
		$map['model'] = array('eq','order');
        $map['record_id'] = array('eq',$id);
        $action_log  = D('ActionLog')->where($map)->select();
        // $sql = D('ActionLog')->getLastSql();
        // var_dump($sql);
        $this->assign('action_log',$action_log);
        $this->meta_title = '订单查看';
        $this->display('cat');
    }
	/**
     * 佣金
     */
        public function yongjin(){  
        $id = I('id');
		$Works  = M('Yewu');
		$map['id'] = $id;
		$data   = $Works->where($map)->field(true)->find();
		if(IS_POST){
			$datas['id'] = $id;
			$datas['akjj'] = I('akjj');
			$datas['cjjj'] = I('cjjj');
			
			 if($Works->where(array('id'=>array('eq',$data['id'])))->save($datas)!== false){
                action_log('yongjin', 'order', $data['id'], UID );
                $this->success('更新成功', U('index'));
            } else {
                $this->error('更新失败');
            }
		}
		$this->assign('data',$data);
		$this->display('yongjin');
    }
	/**
     * 预约到店排单
     */
	public function paidan(){
		//时间生成
        $jrdate = date("Y-m-d", time());
		if(IS_POST){
			$yytime_start = strtotime($_POST['Reserve']['yytime_start']."0:0:0");
			$yytime_end = strtotime($_POST['Reserve']['yytime_end']."23:59:59");
            $map['yytime'] = array('between',array($yytime_start,$yytime_end));
			if($_POST['Reserve']['kf']){
				$map['kf']     = array('eq',$_POST['Reserve']['kf']);
			}
			if($_POST['Reserve']['xsjd']){
				$map['xsjd']     = array('eq',$_POST['Reserve']['xsjd']);
			}
			if($_POST['Reserve']['xxjd']){
				$map['xxjd']     = array('eq',$_POST['Reserve']['xxjd']);
			}
			session('Reserve',I('Reserve'));
            session('paidan',$map);
		}elseif(!I('p')){
			$yytime_start = strtotime($jrdate."0:0:0");
			$yytime_end = strtotime($jrdate."23:59:59");
			$Reserve   = array('yytime_start'=>$jrdate,'yytime_end'=>$jrdate);
            $map['yytime'] = array('between',array($yytime_start,$yytime_end));
			session('Reserve',$Reserve);
            session('paidan',$map);
		}
		$post_data  = session('Reserve');
        $values = session('paidan');
		$data = $this->lists('Yewu',$values,'yytime asc');
		int_to_string($data);
		$this->assign('post_data',$post_data);
        $this->assign('_list',$data);
		$this->display();
	}
    /**
     * 删除数据列表
     */
    public function catdel($yytime_start = '',$yytime_end = '',$yytime = '',$jdtime = '',$kf = '' ,$xsjd = '',$xxjd = '',$ywstase = '',$qdstase = '',$cztime = '',$money = '',$lb = ''){
        $this->meta_title = '业务系统';
        $title       =   I('title');
        
        $map=array();
        //查询数据接收包
        if ($_POST['Reserve']['ddbh']) {
            $map['ddbh'] = array('eq',$_POST['Reserve']['ddbh']);
        }
        if ($_POST['Reserve']['name']) {
            $map['name'] = array('eq',$_POST['Reserve']['name']);
        }
        if ($_POST['Reserve']['phone']) {
            $map['phone'] = array('eq',$_POST['Reserve']['phone']);
        }
        if ($_POST['Reserve']['jdtime_start'] && $_POST['Reserve']['jdtime_end']) {
            $map['jdtime'] = array('between',array(strtotime($_POST['Reserve']['jdtime_start'].'00:00:00'),strtotime($_POST['Reserve']['jdtime_end'].'23:59:59')));
        }
        if ($_POST['Reserve']['yytime_start'] && $_POST['Reserve']['yytime_end']) {
            $map['yytime'] = array('between',array(strtotime($_POST['Reserve']['yytime_start'].'00:00:00'),strtotime($_POST['Reserve']['yytime_end'].'23:59:59')));
        }
        if ($yytime_start && $yytime_end) {
            $map['yytime'] = array('between',array($yytime_start,$yytime_end));
        }
        if ($_POST['Reserve']['ddtime_start'] && $_POST['Reserve']['ddtime_end']) {
            $map['ddtime'] = array('between',array(strtotime($_POST['Reserve']['ddtime_start'].'00:00:00'),strtotime($_POST['Reserve']['ddtime_end'].'23:59:59')));
        }
        if ($_POST['Reserve']['kf']) {
            $map['kf'] = array('eq',$_POST['Reserve']['kf']);
        }
        if ($_POST['Reserve']['jd']) {
            $map['jd'] = array('eq',$_POST['Reserve']['jd']);
        }
        if ($_POST['Reserve']['qd']) {
            $map['qd'] = array('eq',$_POST['Reserve']['qd']);
        }
        if ($_POST['Reserve']['ywstate']) {
            $map['ywstate'] = array('eq',$_POST['Reserve']['ywstate']);
        }
        if ($_POST['Reserve']['lb']) {
            $map['lb'] = array('eq',$_POST['Reserve']['lb']);
        }

        //判断正序、倒序查询
        if ($yytime){
            $paixu['id'] = 'yytime';
            $paixu['state'] =  $yytime;
        }
        if ($jdtime){
            $paixu['id'] = 'jdtime';
            $paixu['state'] =  $jdtime;
        }
        if ($kf){
            $paixu['id'] = 'kf';
            $paixu['state'] =  $kf;
        }
        if ($xsjd){
            $paixu['id'] = 'xsjd';
            $paixu['state'] =  $xsjd;
        }
        if ($xxjd){
            $paixu['id'] = 'xxjd';
            $paixu['state'] =  $xxjd;
        }
        if ($cz){
            $paixu['id'] = 'cz';
            $paixu['state'] =  $cz;
        }
        if ($yystase){
            $paixu['id'] = 'yystase';
            $paixu['state'] =  $yystase;
        }
        if ($qdstase){
            $paixu['id'] = 'qd';
            $paixu['state'] =  $qdstase;
        }
        if ($lb){
            $paixu['id'] = 'lb';
            $paixu['state'] =  $lb;
        }
        if ($money){
            $paixu['id'] = 'money';
            $paixu['state'] =  $money;
        }
     

        //排序
        if($paixu['state'] == asc){
            $paixuid = $paixu['id'];
            $data_yw=$this->lists('YewuCopy',$map,"$paixuid asc");
        }else if($paixu['state'] == desc){
            $paixuid = $paixu['id'];
            $data_yw=$this->lists('YewuCopy',$map,"$paixuid desc");
        }else{
            $data_yw=$this->lists('YewuCopy',$map,"id desc");
        }


        $ywstate = D('Ywlb')->where(array('status'=>array('eq',1)))->field('uid,name')->select();
        $kf = D('Member')->field('uid,nickname')->select();
        //重组数据获取客服、接待、状态名称
        foreach ($data_yw as $key => $value) {
            foreach ($ywstate as $key => $v) {
                if ($value['ywstate'] == $v['uid']) {
                   $value['ywstate'] = $v['name'];
                }
                if ($value['qd'] == $v['uid']) {
                   $value['qd'] = $v['name'];
                }
                if ($value['lb'] == $v['uid']) {
                   $value['lb'] = $v['name'];
                }
            }
            foreach ($kf as $key => $s) {
                if ($value['kf'] == $s['uid']) {
                   $value['kf'] = $s['nickname'];
                }
                if ($value['xsjd'] == $s['uid']) {
                   $value['xsjd'] = $s['nickname'];
                }
                if ($value['xxjd'] == $s['uid']) {
                   $value['xxjd'] = $s['nickname'];
                }
            }
          
            $data[] = $value;
        }
        int_to_string($data);
        $this->assign('_list',$data);

        

        //业务查询记录参数。
        $post_data  =I('post.Reserve');
        $this->assign('post_data',$post_data);
		
        //时间生成
        $jrdate = date("Y-m-d", time());
        $jryydate['start'] = strtotime($jrdate."0:0:0");
        $jryydate['end'] = strtotime($jrdate."24:00:00");
        $this->assign('jryydate',$jryydate); 
        $this->display();
    }
    /**
    * 查看
    */
    public function catd($id = 0){
        $Works = D('YewuCopy');

        $data = $Works->where("id = %d",array($id))->find();
        $this->assign('data', $data);
        $this->meta_title = '订单查看';
        $this->display('cat');
    }
	



}
